/*
 * TextFieldComponent.java
 *
 * Created on June 6, 2007, 11:59 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.javatech.jmf.components.textField.server;

import info.javatech.jmf.common.components.TextFieldConstants;
import info.javatech.jmf.server.PCommandOwner;
import info.javatech.jmf.server.PComponentHolder;
import info.javatech.jmf.server.PItemOwner;
import info.javatech.jmf.server.protocol.PCommand;
import info.javatech.jmf.server.protocol.PComponent;
import info.javatech.jmf.server.protocol.PItem;
import info.javatech.jmf.server.util.EncodeUtils;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.faces.FacesException;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.el.ValueExpression;
import javax.el.ELContext;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author afedotov
 */
public class TextFieldComponent extends UIInput implements PComponentHolder, PCommandOwner{
    
    private List<PCommand> commands;
    
    /** Creates a new instance of TextFieldComponent */
    public TextFieldComponent() {
        setRendererType(null);
        commands = new LinkedList<PCommand>();
    }
    
    public String getRendererType() {
        return null;
    }
    
    public String getFamily() {
        return TextFieldConstants.TYPE_NAME;
    }
    
    public void encodeEnd(FacesContext context) throws IOException {
        PComponent pc = getPComponent();
        
        if (getParent() instanceof PItemOwner) {
            ((PItemOwner)getParent()).addItem((PItem) pc);
        } else throw new FacesException("Parent of this component is not instance of PItemwner");
    }
    
    public void decode(FacesContext context) {
        String clientId = getId();
        
        String persistedComponent = context.getExternalContext().getRequestParameterValuesMap().get(clientId)[0];
        
        PComponent textField = (PComponent) EncodeUtils.decode(persistedComponent);
        
        this.setSubmittedValue(textField.getPropertyValues()[0]);
    }
    
    public PComponent getPComponent() {
        PItem textField = new PItem();
        textField.setId(getId());
        
        textField.setType(TextFieldConstants.TYPE_NAME);
        
        PCommand[] pcommands = new PCommand[commands.size()];
        int i = 0;
        for(PCommand command:commands)
            pcommands[i++] = command;
        textField.setCommands(pcommands);
        
        
        int constrains = 0;
        
        Map<String, Object> atts = getAttributes();
        
        if (atts.get("isPassword")!=null&&(Boolean)atts.get("isPassword")) constrains = constrains | TextFieldConstants.ISPASSWORD;
        if (atts.get("isUneditable")!=null&&(Boolean)atts.get("isUneditable")) constrains = constrains | TextFieldConstants.ISUNEDITABLE;
        if (atts.get("isSensitive")!=null&&(Boolean)atts.get("isSensitive")) constrains = constrains | TextFieldConstants.ISSENSITIVE;
        if (atts.get("isNonPredictive")!=null&&(Boolean)atts.get("isNonPredictive")) constrains = constrains | TextFieldConstants.ISNONPREDICTIVE;
        if (atts.get("isNonInitialCapsWord")!=null&&(Boolean)atts.get("isNonInitialCapsWord")) constrains = constrains | TextFieldConstants.ISINITIALCAPSWORD;
        if (atts.get("isNonInitialCapsSentense")!=null&&(Boolean)atts.get("isNonInitialCapsSentense")) constrains = constrains | TextFieldConstants.ISINITIALCAPSSENTENSE;
        
        if (atts.get("type")!=null)
            constrains = constrains | (Integer)atts.get("type");
        
        textField.setPropertyNames(
                new String[]
        {
            TextFieldConstants.LABEL_PROPERTY_NAME,
            TextFieldConstants.STRING_PROPERTY_NAME,
            TextFieldConstants.MAXIMUMSIZE_PROPERTY_NAME,
            TextFieldConstants.CONSTRAINS_PROPERTY_NAME,
            TextFieldConstants.INITIALINPUTMODE_PROPERTY_NAME,
            TextFieldConstants.PREFFEREDHSIZE_PROPERTY_NAME,
            TextFieldConstants.PREFFEREDVSIZE_PROPERTY_NAME,
            TextFieldConstants.DEFAULTCOMMAND_PROPERTY_NAME
        });
        
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        
        textField.setPropertyValues(
                new String[]
        {
            StringUtils.defaultIfEmpty((String)getAttributes().get("label"),""),
            (String)ObjectUtils.defaultIfNull(getValue(),""),
            ((Integer)ObjectUtils.defaultIfNull(getAttributes().get("maximumSize"),0)).toString(),
            String.valueOf(constrains),
            StringUtils.defaultIfEmpty((String)getAttributes().get("initialInputMode"),""),
            ((Integer)ObjectUtils.defaultIfNull(getAttributes().get("prefferedHSize"),0)).toString(),
            ((Integer)ObjectUtils.defaultIfNull(getAttributes().get("prefferedVSize"),0)).toString(),
            StringUtils.defaultIfEmpty((String)getAttributes().get("defaultCommand"),"")
        });
        
        return textField;
    }
    
    public void addCommand(PCommand command) {
        commands.add(command);
    }
    
    public void resetCommandsContainer() {
        commands.clear();
    }
    
}
